<!DOCTYPE html>

<html>

	<head>
		<script src="js/three.js"></script>
		<script src="js/Detector.js"></script>
		<script src="js/Stats.js"></script>
		<script src="js/OrbitControls.js"></script>
		<script src="js/KeyboardState.js"></script>
		<style>
			html, body {
				margin: 0;
			}
		</style>
	</head>

	<body>
		<div id="ThreeJS"></div>
		<script>
			// Rotate an object around an arbitrary axis in object space
			var rotObjectMatrix;
			function rotateAroundObjectAxis(object, axis, radians) {
				rotObjectMatrix = new THREE.Matrix4();
				rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);

				// old code for Three.JS pre r54:
				// object.matrix.multiplySelf(rotObjectMatrix);      // post-multiply
				// new code for Three.JS r55+:
				object.matrix.multiply(rotObjectMatrix);

				// old code for Three.js pre r49:
				// object.rotation.getRotationFromMatrix(object.matrix, object.scale);
				// new code for Three.js r50+:
				object.rotation.setEulerFromRotationMatrix(object.matrix);
			}

			var rotWorldMatrix;
			// Rotate an object around an arbitrary axis in world space
			function rotateAroundWorldAxis(object, axis, radians) {
				rotWorldMatrix = new THREE.Matrix4();
				rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);

				// old code for Three.JS pre r54:
				//  rotWorldMatrix.multiply(object.matrix);
				// new code for Three.JS r55+:
				rotWorldMatrix.multiply(object.matrix);
				// pre-multiply

				object.matrix = rotWorldMatrix;

				// old code for Three.js pre r49:
				// object.rotation.getRotationFromMatrix(object.matrix, object.scale);
				// old code for Three.js pre r59:
				// object.rotation.setEulerFromRotationMatrix(object.matrix);
				// code for r59+:
				object.rotation.setFromRotationMatrix(object.matrix);
			}

			/*
			Three.js "tutorials by example"
			Author: Lee Stemkoski
			Date: July 2013 (three.js v59dev)
			*/

			// MAIN

			// standard global variables
			var container, scene, camera, renderer, controls, stats;
			var keyboard = new KeyboardState();
			var clock = new THREE.Clock();

			// custom global variables
			var starship;
var OMGWTFBBQ;
			init();
			animate();

			// FUNCTIONS
			function init() {
				// SCENE
				scene = new THREE.Scene();
				// CAMERA
				var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
				var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
				camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
				scene.add(camera);
				camera.position.set(0, 5, 20);
				camera.lookAt(scene.position);
				// RENDERER
				if (Detector.webgl)
					renderer = new THREE.WebGLRenderer({
						antialias : true
					});
				else
					renderer = new THREE.CanvasRenderer();
				renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
				container = document.getElementById('ThreeJS');
				container.appendChild(renderer.domElement);
				// EVENTS
				/* THREEx.WindowResize(renderer, camera);
				THREEx.FullScreen.bindKey({
				charCode : 'm'.charCodeAt(0)
				}); */
				// CONTROLS
				/* controls = new THREE.OrbitControls(camera, renderer.domElement); */
				// STATS
				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.bottom = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild(stats.domElement);
				// LIGHT
				var light = new THREE.PointLight(0xffffff);
				light.position.set(100, 250, 100);
				scene.add(light);

				////////////
				// CUSTOM //
				////////////

				var objectLoader = new THREE.ObjectLoader();
				objectLoader.load("models/starship.object.json", function(objStarship) {
					//var material = new THREE.MeshFaceMaterial( materials );
					starship = objStarship;
					//starship.scale.set(20, 20, 20);
					//rotateAroundWorldAxis(starship, new THREE.Vector3(0, 1, 0), -Math.PI / 2);
					//starship.rotation.y = -Math.PI / 2;
					//starship.position.y = 70;
					//starship.rotateOnAxis(new THREE.Vector3(0, 1, 0), -Math.PI / 2)
					scene.add(starship);
				});

				/* for (var i = 0; i < 20; i++) {
				 objectLoader.load("models/earth.object.json", function(obj) {
				 obj.position.x = Math.random() * 500 - 250;
				 obj.position.y = Math.random() * 500 - 250;
				 obj.position.z = Math.random() * 500 - 250;//, Math.random() * 1000 - 500, Math.random() * 1000 - 500));
				 console.log(obj.position);
				 obj.scale.set(10, 10, 10);
				 scene.add(obj);
				 });
				 } */

	

				var objectLoader2 = new THREE.ObjectLoader();
				objectLoader2.load("models/earth.object.json", function(objEarth) {
					console.log(arguments);
					//for (var i = 0; i < 200; i++) {
						OMGWTFBBQ = objEarth;
						var pos = new THREE.Vector3(
							Math.random() * 500 - 250,
							Math.random() * 500 - 250,
							Math.random() * 500 - 250
						);
						OMGWTFBBQ.position.setX(pos.x);
						OMGWTFBBQ.position.setY(pos.y);
						OMGWTFBBQ.position.setZ(pos.z);
//						earth.position.x = Math.random() * 500 - 250;
	//					earth.position.y = Math.random() * 500 - 250;
		//				earth.position.z = Math.random() * 500 - 250;
						//, Math.random() * 1000 - 500, Math.random() * 1000 - 500));
						console.log(OMGWTFBBQ.position);
						OMGWTFBBQ.scale.set(20, 20, 20);
						scene.add(OMGWTFBBQ);
					//}
				});

				var imagePrefix = "images/spacebox";
				var directions = ["+x", "-x", "+y", "-y", "+z", "-z"];
				var imageSuffix = ".png";
				var skyGeometry = new THREE.CubeGeometry(500, 500, 500);

				var materialArray = [];
				for (var i = 0; i < 6; i++)
					materialArray.push(new THREE.MeshBasicMaterial({
						map : THREE.ImageUtils.loadTexture(imagePrefix + directions[i] + imageSuffix),
						side : THREE.BackSide
					}));
				var skyMaterial = new THREE.MeshFaceMaterial(materialArray);
				skyBox = new THREE.Mesh(skyGeometry, skyMaterial);
				scene.add(skyBox);

			}

			function animate() {
				requestAnimationFrame(animate);
				render();
				update();
			}

			var acceleration = 0;

			function update() {
				var delta = clock.getDelta();

				acceleration += delta / 25;

				acceleration = acceleration > 2 ? 2 : acceleration;

				var rotateAngle = Math.PI / 2 * delta / 1;

				if (starship !== undefined) {
					starship.translateZ(-acceleration);

					keyboard.update();

					if (keyboard.pressed("left")) {
						// do something
						starship.rotateOnAxis(new THREE.Vector3(0, 0, 1), rotateAngle * 1.5);
					}
					if (keyboard.pressed("right")) {
						starship.rotateOnAxis(new THREE.Vector3(0, 0, 1), -rotateAngle * 1.5);
					}
					if (keyboard.pressed("up")) {
						starship.rotateOnAxis(new THREE.Vector3(1, 0, 0), -rotateAngle);
					}
					if (keyboard.pressed("down")) {
						starship.rotateOnAxis(new THREE.Vector3(1, 0, 0), rotateAngle);
					}

					var relativeCameraOffset = new THREE.Vector3(0, 5, 20);

					var cameraOffset = relativeCameraOffset.applyMatrix4(starship.matrixWorld);

					camera.position.x = cameraOffset.x;
					camera.position.y = cameraOffset.y;
					camera.position.z = cameraOffset.z;
					camera.lookAt(starship.position);

					skyBox.position.x = starship.position.x;
					skyBox.position.y = starship.position.y;
					skyBox.position.z = starship.position.z;

					//console.log(starship.position)
				}

				stats.update();

			}

			function render() {
				renderer.render(scene, camera);
			}

		</script>
	</body>

</html>
